use bracket_lib::prelude::*;
use image::*;
use crate::player::Player;

pub struct BoomBall {
    pub x: i32,
    pub y: i32,
    pub img: DynamicImage,
}

impl BoomBall {
    pub fn new(x: i32, y: i32) -> Self {
        BoomBall {
            x,
            y,
            img: image::open("assets/boom.png").unwrap(),
        }
    }
    pub fn render(&self, ctx: &mut BTerm, camera_x: i32) {
        let (img_width, img_height) = self.img.dimensions();
        let screen_x = self.x - camera_x;
        for dx in 0..6 {
            for dy in 0..6 {
                let px = screen_x + dx;
                let py = self.y + dy;
                if px >= 0 && px < 120 && py >= 0 && py < 80 {
                    let img_x = (dx as f32 / 6.0 * img_width as f32) as u32;
                    let img_y = (dy as f32 / 6.0 * img_height as f32) as u32;
                    let img_x = img_x.min(img_width - 1);
                    let img_y = img_y.min(img_height - 1);
                    let pixel = self.img.get_pixel(img_x, img_y);
                    ctx.set_bg(px, py, (pixel[0], pixel[1], pixel[2]));
                }
            }
        }
    }
    // 判定玩家中心4x4与boom中心4x4重叠
    pub fn check_collision(&self, player: &Player) -> bool {
        let player_cx = player.x + 1;
        let player_cy = player.y + 1;
        let boom_cx = self.x + 1;
        let boom_cy = self.y + 1;
        // 4x4区域重叠
        player_cx < boom_cx + 4 && player_cx + 4 > boom_cx &&
        player_cy < boom_cy + 4 && player_cy + 4 > boom_cy
    }
}

pub struct LiveBall {
    pub x: i32,
    pub y: i32,
    pub img: DynamicImage,
}

impl LiveBall {
    pub fn new(x: i32, y: i32) -> Self {
        LiveBall {
            x,
            y,
            img: image::open("assets/live_ball.png").unwrap(),
        }
    }
    pub fn render(&self, ctx: &mut BTerm, camera_x: i32) {
        let (img_width, img_height) = self.img.dimensions();
        let screen_x = self.x - camera_x;
        for dx in 0..6 {
            for dy in 0..6 {
                let px = screen_x + dx;
                let py = self.y + dy;
                if px >= 0 && px < 120 && py >= 0 && py < 80 {
                    let img_x = (dx as f32 / 6.0 * img_width as f32) as u32;
                    let img_y = (dy as f32 / 6.0 * img_height as f32) as u32;
                    let img_x = img_x.min(img_width - 1);
                    let img_y = img_y.min(img_height - 1);
                    let pixel = self.img.get_pixel(img_x, img_y);
                    ctx.set_bg(px, py, (pixel[0], pixel[1], pixel[2]));
                }
            }
        }
    }
    // 判定玩家中心4x4与live_ball中心4x4重叠
    pub fn check_collision(&self, player: &Player) -> bool {
        let player_cx = player.x + 1;
        let player_cy = player.y + 1;
        let live_cx = self.x + 1;
        let live_cy = self.y + 1;
        // 4x4区域重叠
        player_cx < live_cx + 4 && player_cx + 4 > live_cx &&
        player_cy < live_cy + 4 && player_cy + 4 > live_cy
    }
} 